/**
 * @param {number[]} pushed
 * @param {number[]} popped
 * @return {boolean}
 */
 var validateStackSequences = function(pushed, popped) {
    let stack = [];
    while(popped.length){
        let length=stack.length;
        if(length && stack[length-1] == popped[0]){
            stack.pop();
            popped.shift();
        }
        else{
            if(!pushed.length){
                return false;
            }
            stack.push(pushed.shift())
        }
    }
    return true;
};

console.log(validateStackSequences([1,2,3,4,5],[4,5,3,2,1]))